package com.sportizz.sport;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.graphics.PorterDuff.Mode;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;

public class SubMenu extends BaseAdapter {

	Context context;
	List<String> data;

	public SubMenu(Context applicationContext, List<String> data_sub) {

		this.context = applicationContext;
		this.data = data_sub;
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		View v;

		if (convertView == null) {
			v = inflater.inflate(R.layout.menu_list, null);
		} else {
			v = convertView;
		}

		
		Typeface sport_font = Typeface.createFromAsset(context.getAssets(),
				"hel57.ttf");
		
		TextView menu_name = (TextView) v.findViewById(R.id.Menu_name);

		String[] sport_name = data.get(position).split(":");

		menu_name.setText(sport_name[0]);
		menu_name.setTypeface(sport_font);
		
		ImageView menu_image = (ImageView) v.findViewById(R.id.image_menu);

		menu_image.setImageBitmap(bitmap_image(sport_name[1]));
		
		return v;
	}

	private Bitmap bitmap_image(String image_code) {
		InputStream is1;
		Bitmap output1 = null;
		try {
			is1 = context.getResources().getAssets()
					.open("sports/" + image_code + ".png");

			Bitmap res1 = BitmapFactory.decodeStream(is1);

			Log.i("suraj", "champ =" + res1);

			output1 = Bitmap.createBitmap(res1.getWidth(), res1.getHeight(),
					Bitmap.Config.ARGB_8888);
			Canvas canvas1 = new Canvas(output1);

			Paint paint = new Paint();
			Rect rect = new Rect(0, 0, res1.getWidth(), res1.getHeight());
			RectF rectF = new RectF(rect);
			float roundPx1 = 8f;

			paint.setAntiAlias(true);
			canvas1.drawARGB(0, 0, 0, 0);
			paint.setColor(0xff424242);
			canvas1.drawRoundRect(rectF, roundPx1, roundPx1, paint);
			paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
			canvas1.drawBitmap(res1, rect, rect, paint);
		} catch (IOException e) {
			e.printStackTrace();
			try {
				is1 = context.getResources().getAssets()
						.open("sports/GAM.png");

			Bitmap res1 = BitmapFactory.decodeStream(is1);

			Log.i("suraj", "champ =" + res1);

			output1 = Bitmap.createBitmap(res1.getWidth(), res1.getHeight(),
					Bitmap.Config.ARGB_8888);
			Canvas canvas1 = new Canvas(output1);

			Paint paint = new Paint();
			Rect rect = new Rect(0, 0, res1.getWidth(), res1.getHeight());
			RectF rectF = new RectF(rect);
			float roundPx1 = 8f;

			paint.setAntiAlias(true);
			canvas1.drawARGB(0, 0, 0, 0);
			paint.setColor(0xff424242);
			canvas1.drawRoundRect(rectF, roundPx1, roundPx1, paint);
			paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
			canvas1.drawBitmap(res1, rect, rect, paint);
			} catch (IOException e1) {
			}
		}
		return output1;
	}

}
